package cn.gqr.controller;

import cn.gqr.entity.Largefile;
import cn.gqr.entity.User;
import cn.gqr.service.UserService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gqr
 * @company null
 * @create 2022-06-11 16:53
 */
@RestController
@RequestMapping("/api/adm")
public class UserAdminController {

    @Autowired
    private UserService userService;

    //获取所有买家信息
    @PreAuthorize("hasAuthority('adm:queryAllUsers')")
    @RequestMapping("/user/queryAllUsers/{start}/{num}")
    public Map<String,Object> queryAllUsers(@PathVariable("start")int start, @PathVariable("num")int num){
        return RespJson.pack(200,"所有买家信息",userService.queryAllUsers(start, num));
    }


    //注册管理员
    @PreAuthorize("hasAuthority('adm:registerAdmin')")
    @RequestMapping("/user/registerAdmin")
    public Map<String,Object> registerAdmin(User user, @RequestParam(value = "pic",required = false) MultipartFile file){
        boolean b = userService.registerAdmin(user, file);
        return RespJson.pack(200,b?"注册成功":"未知错误",null);
    }

    //买家账号解冻
    @PreAuthorize("hasAuthority('adm:unfreezeUser')")
    @RequestMapping("/user/unfreezeUser/{uid}")
    public Map<String,Object> unfreezeUser(@PathVariable("uid")int uid){
        boolean b = userService.unfreezeUserOrFrozenAccount(uid, 0);
        return RespJson.pack(200, b?"解冻成功":"未知错误",null);
    }

    //买家账号冻结
    @PreAuthorize("hasAuthority('adm:frozenAccount')")
    @RequestMapping("/user/frozenAccount/{uid}")
    public Map<String,Object> frozenAccount(@PathVariable("uid")int uid){
        boolean b = userService.unfreezeUserOrFrozenAccount(uid, 1);
        return RespJson.pack(200, b?"冻结成功":"未知错误",null);
    }


    //根据id查询买家信息
    @PreAuthorize("hasAuthority('adm:selectUserById')")
    @RequestMapping("/user/selectUserById/{id}")
    public Map selectUserById(@PathVariable("id")int id){
        User user = userService.selectUserById(id);
        return RespJson.pack(200, "用户信息",user);
    }


}
